<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class YangshengController extends BaseController {

	/*养生列表页*/
    public function index(){
    	if(!empty(I('get.type'))){
    		$id = I('get.type');
    	}else{
    		$id = 1;
    	}
        /*根据根ID获取模板*/
    	$resource = D(C('TABLE_RESOURCE'));
    	$res = $resource->where(array('pid'=>0))->find($id);
    	if($res){
			$this->getbannertoresource($id);//获取分类下面的banner图
    		$tpl = $res['tpl'];
    		$datatitle = $res['typename'];
    		$this->assign('datalist',$this->getallinfocontent($id));
    		$this->assign('datainfo',$res);
    		$this->getallresourceinfo();
    		$this->assign('doid',$id);
			if($id==27){
				if(ismobile()){
					$this->display('vhuiketing_mobile');
				}else{
					$this->display('vhuiketing');
				}
			}else if($id==103){
				if(ismobile()){
					$this->display('zuizhongqiu_mobile');
				}else{
					$this->display('zuizhongqiu');
				}
			}else{
				$this->display($tpl);
			}
    	}else{
    		echo "参数错误！";
    	}
    }

	//根据资源分类获取所属广告图
	protected function getbannertoresource($id){
		$resource = D(C('TABLE_RESOUVRCEBANNER'));
		$res = $resource->where(array('resid'=>$id))->order('bannersort asc')->select();
		$this->assign('bannerlist',$res);
	}
    
    /*
     * ajax来访问需获取的数据
     */
    public function getajaxtodata(){
    	$id = I('post.id');
    	if(!empty($id)){
    		$this->getallinfocontent($id,true);
    	}
    }
    
    /*
     * 获取全部栏目分类
     */
    protected function getallresourceinfo(){
    	$resource = D(C('TABLE_RESOURCE'));
    	$res = $resource->select();
    	if($res){
    		$this->assign('allinfo',$res);	
    	}
    }
    
    /*
     * 获取所有分类内容信息
     * $typeid 分类id号
     */
    protected function getallinfocontent($id,$a=null){
    	$ys = D(C('TABLE_YANGSHENG'));
    	$where['y_state'] = 0;
    	$where['type'] = array('in',$this->getresourceid($id));
    	$where['_logic'] = 'and';
    	$result = $ys->where($where)->order('sn desc,y_datetime desc')->select();
		//dump($result);die;
    	if($result){
    		if($a){
    			print_r($result);
    		}else{
    			return $result;
    		}
    	}
    }
    
    /*
     * 根据id获取所有的资源所属id
     */
    protected function getresourceid($id){
        $resource = D(C('TABLE_RESOURCE'));
    	$res = $resource->where(array('pid'=>$id))->select();
    	if($res){
    		$arrid = null;
    		foreach ($res as $v){
    			$arrid .= $v['id'].',';
    		}
    		$resultid = rtrim($arrid,',');
    	}else{
    		$resultid = ''.$id.'';
    	}
    	return $resultid;
    }
    
    /*
     * 养生详情页
     */
    public function ysarticle(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$ys = D(C('TABLE_YANGSHENG'));
    		$result = $ys->find($id);
    		if($result['y_state']==0){
    			$vurl = $result['y_vurl'];
    			preg_match_all('/uu=([^<]*)&vu/is', $vurl, $uu);
    			preg_match_all('/vu=([^<]*)&pu/is', $vurl, $vu);
    			$result['uu'] = $uu[1][0];
    			$result['vu'] = $vu[1][0];
    			$this->assign('ysarticle',$result);
    			$this->display();
    		}else{
    			echo "本条数据被删除！";
    		}
    		
    	}else{
    		$this->error('非法参数！','',1);
    	}
    	
    }

	public function pcarticle(){
		$id = I('get.id');
		$this->getresinfo('mobilearticle',$id);
	}
	public function pcarticlezq(){
		$id = I('get.id');
		$this->getresinfo('mobilearticlezq',$id);
	}

	protected function getresinfo($tpl,$id=null){
		if(!empty($id)){
			$ys = D(C('TABLE_YANGSHENG'));
			$result = $ys->find($id);
			if($result['y_state']==0){
				$filename = getLetvArray($result['y_vurl'],'putfile');
				$filename = iconv("gbk","utf-8",urldecode($filename));
				$result['videofilename'] = $filename;
				$this->assign('postiurl',getURL());
				$this->assign('pcarticle',$result);
				if(ismobile()){
					$this->display($tpl);
				}else{
					$this->display();
				}
			}else{
				echo "本条数据被删除！";
			}
		}else{
			$this->error('非法参数！','',1);
		}
	}

	/*
	 * V会客厅搜索
	 */
	public function search(){
		$type = I('get.type');
		$key = I('post.q');
		$vhkt = D(C('TABLE_YANGSHENG'));
		$res = $vhkt->where("(y_title like '%".$key."%' or y_pname like '%".$key."%') and type=$type")->select();
		$this->assign('datalist',$res);
		if($type==27){
			if(ismobile()){
				$this->display('vhuiketing_mobile');
			}else{
				$this->display('vhuiketing');
			}
		}
		if($type==103){
			if(ismobile()){
				$this->display('zuizhongqiu_mobile');
			}else{
				$this->display('zuizhongqiu');
			}
		}

	}


}